
# Clear screen and fill with a background color
def background (c):
    screen.fill(c)

# Return the distance between two points
def distance (a, b):
    d = (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1])
    return math.sqrt(d)

# Draw an arc, a fraction of an ellipse, and fill it.
def ell (cx, cy, w, h, a1, a2):
    t = a1
    x0 = cx + w/2* math.cos(-t)
    y0 = cy + h/2* math.sin(-t)
    while t < a2:
#    for t in range(a1, a2):
        print(x0, y0)
        t = t + 0.01
        x1 = cx + w/2 * math.cos(-t)
        y1 = cy + h/2 * math.sin(-t)
        pygame.draw.line(screen, (100, 0, 0), (x0,y0), (x1,y1), 2)
        x0 = x1
        y0 = y1

# Draw a small hexagonal grid
def hex (r):
    h = 2*r
    w = math.sqrt(3.0)*r
    x = w
    y = h
    for i in range(0,5,2):  # vertical
        y = 3*h/4 * (i+1)
        for j in range(0,10):
            regular_polygon (j*w, y, r, 6)
        y = 3*h/4 * (i+2)
        for j in range (0,10):
            regular_polygon (w/2+j*w, y, r, 6)

# Read a sound file and create a playable object
def loadSound (s):
    if mixer.get_init() == False:
        mixer.pre_init(buffersize=512)
        mixer.init()
    try:
        m = mixer.Sound(s)
    except:
        print ("Problem loading the sound file '", s, "'")
        m = None
    return m 

# Draw a regular polygon with N sides.
def regular_polygon (xc, yc, r, n):
    pi = 3.1415926
    pi2 = pi/2
    x0 = xc + r
    y0 = yc
    verts = []
    a = 2*pi/n
    for i in range(0,n):
        x0 = xc + math.cos(pi2+a*i) * r
        y0 = yc + math.sin(pi2+a*i) * r
        verts.append([x0,y0])
    pygame.draw.polygon(screen, (0,0,0), verts, 2)

# Save the drawing area to a file.
def svae_screen(s):
    pygame.image.save (screen, s)

# Draw a text string at the given point.                       **
def text (s, x, y, size=14, f=None):
    if f == None:                   # Create a font if needed
        f = pygame.font.SysFont(None, size)
    text = f.render(s, 1, (0,0,0))  # Render the string in black
    screen.blit(text, (x, y))

# Draw a triangle specified by three points.                   **
def triangle (x0,y0, x1,y1, x2,y2, fill=None):
    if fill is not None:
        pygame.draw.polygon (screen, fill, ((x0,y0), (x1,y1), (x2,y2)), 0)
    else
        pygame.draw.polygon (screen, (0,0,0), ((x0,y0), (x1,y1), (x2,y2)), 1)

